﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class TempPOItemRepository
    {
        StationeryInventoryEntities1 ent;

        public TempPOItemRepository()
        {
            ent = new StationeryInventoryEntities1();
        }

        #region SelectMethod
        public TempPOItem SelectTempPOItemByNewTempPOItem(TempPOItem newTempPOItem)
        {
            TempPOItem newTempPO = new TempPOItem();

            var temp = ent.TempPOItems.Where(x => x.Status == newTempPOItem.Status && x.ClerkID == newTempPOItem.ClerkID && x.ItemCode == newTempPOItem.ItemCode && x.SupplierID == newTempPOItem.SupplierID);

            if (temp != null)
            {
                newTempPO = temp.FirstOrDefault();
            }
            return newTempPO;
        }

        public List<TempPOItem> GetTempPOItemByClerkIdAndSupplierId(int clerkId, string supplierId)
        {
            List<TempPOItem> list = new List<TempPOItem>();

            var temp = ent.TempPOItems.Where(x => x.SupplierID == supplierId && x.ClerkID == clerkId);

            if (temp != null)
            {
                list = temp.ToList();
            }
            return list;
        }

        public TempPOItem GetTempPOItemByTempPOId(int tempPOId)
        {
            TempPOItem tempPOItem = new TempPOItem();

            var temp = ent.TempPOItems.Where(x => x.TempPOID == tempPOId);

            if (temp != null)
            {
                tempPOItem = temp.FirstOrDefault();
            }
            return tempPOItem;
        }

        public List<TempPOItem> GetTempPOItemBySupplierId(string supplierId)
        {
            List<TempPOItem> list = new List<TempPOItem>();

            var temp = ent.TempPOItems.Where(x => x.SupplierID == supplierId);

            if (temp != null)
            {
                list = temp.ToList();
            }
            return list; 
        }

        public List<TempPOItem> GetTempPOItem()
        {
            List<TempPOItem> list = new List<TempPOItem>();

            var temp = ent.TempPOItems;

            if (temp != null)
            {
                list = temp.ToList();
            }
            return list;
        }
        #endregion

        #region Create Method
        public void CreateTempPOItem(TempPOItem newTempPOItem)
        {
            ent.TempPOItems.Add(newTempPOItem);
            ent.SaveChanges();
        }
        #endregion

        #region Update Method
        public void UpdateTempPOItem(TempPOItem tempPOItem)
        {
            var temp = ent.TempPOItems.FirstOrDefault(x => x.TempPOID == tempPOItem.TempPOID);

            temp.ItemCode = tempPOItem.ItemCode;
            temp.Quantity = tempPOItem.Quantity;
            temp.SupplierID = tempPOItem.SupplierID;
            temp.ClerkID = tempPOItem.ClerkID;
            temp.Date = tempPOItem.Date;
            temp.Status = tempPOItem.Status;

            ent.SaveChanges();
        }
        #endregion

        public void DeleteTempPOItem(int tempPOId)
        {
            var temp = ent.TempPOItems.FirstOrDefault(x => x.TempPOID == tempPOId);

            ent.TempPOItems.Remove(temp);
            ent.SaveChanges();
        }
    }
}
